home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Cache / HTTP_Request.php < prev    next >
PHP Script  |  2004-10-01  |  10KB  |  297 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Fabien MARTY <fabien.marty@free.fr> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: HTTP_Request.php,v 1.9 2003/07/13 08:27:18 fab Exp $
  19.  
  20. require_once 'Cache.php';
  21. require_once 'HTTP/Request.php';
  22.  
  23. define('CACHE_HTTP_REQUEST_GROUP_NAME', 'cache_http_request');
  24. define('CACHE_HTTP_REQUEST_SUCCESS_RESPONSE_CODE', 200);
  25. define('CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY', 1);
  26. define('CACHE_HTTP_REQUEST_RETURN_FALSE', 2);
  27. define('CACHE_HTTP_REQUEST_RETURN_PEAR_ERROR', 3);
  28.  
  29. /**
  30. * HTTP_Request Cache
  31. *
  32. * The classical example is :
  33. *
  34. * You want to get news from another site through RSS remote files. But you
  35. * don't want to access to to the remote site at every time you display
  36. * its news on your site. Because, if the remote site is down or slow...
  37. * So you you need a class which makes a local cache copy of the remote file.
  38. * Every x hours, the cache is updated. But if the remote site is down, the
  39. * local cache copy is keeped (you can also get error messages if you want).
  40. *
  41. * So you need this class!
  42. *
  43. * Cache_HTTP_Request inherits from Cache and use HTTP_Request to access to
  44. * the remote file.
  45. *
  46. * Usage example :
  47. *
  48. * <?php
  49. * require_once('Cache/HTTP_Request.php');
  50. *
  51. * $cache = &new Cache_HTTP_Request('http://www.php.net', NULL, 'file', NULL, 3600);
  52. * $cache->sendRequest();
  53. * $remoteFileBody = $cache->getResponseBody();
  54. *
  55. * (...)
  56. * ?>
  57. *
  58. * @author   Fabien MARTY <fabien.marty@free.fr>
  59. * @version  $Id: HTTP_Request.php,v 1.9 2003/07/13 08:27:18 fab Exp $
  60. * @package  Cache
  61. */
  62.  
  63. class Cache_HTTP_Request extends Cache
  64. {
  65.  
  66.     // --- Private properties ---
  67.  
  68.     /**
  69.     * Lifetime in seconds (0 endless)
  70.     *
  71.     * @var int $_expires
  72.     */
  73.     var $_expires;
  74.  
  75.     /**
  76.     * HTTP Request
  77.     *
  78.     * @var object $_request
  79.     */
  80.     var $_request;
  81.  
  82.     /**
  83.     * Cache id for the classic cache file
  84.     *
  85.     * @see sendRequest()
  86.     * @var string $_id
  87.     */
  88.     var $_id;
  89.  
  90.     /**
  91.     * Cache id for the endless cache file
  92.     *
  93.     * @see sendRequest()
  94.     * @var string $_id
  95.     */
  96.     var $_id2;
  97.  
  98.     /**
  99.     * Data to use
  100.     *
  101.     * @see getReponseBody(), getReponseHeader(), getReponseCode()
  102.     * @var array $_data
  103.     */
  104.     var $_data ;
  105.  
  106.     // --- Public methods ---
  107.  
  108.     /**
  109.     * Constructor
  110.     *
  111.     * @param $url The url to access
  112.     * @param $params Associative array of parameters which can be:
  113.     *                  method     - Method to use, GET, POST etc
  114.     *                  http       - HTTP Version to use, 1.0 or 1.1
  115.     *                  user       - Basic Auth username
  116.     *                  pass       - Basic Auth password
  117.     *                  proxy_host - Proxy server host
  118.     *                  proxy_port - Proxy server port
  119.     *                  proxy_user - Proxy auth username
  120.     *                  proxy_pass - Proxy auth password
  121.     * @param string $container Name of container class
  122.     * @param array $containerOptions Array with container class options
  123.     * @param int $mode What to do when the remote server is down :
  124.     *                   CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY or
  125.     *                   CACHE_HTTP_REQUEST_RETURN_FALSE or
  126.     *                   CACHE_HTTP_REQUEST_RETURN_PEAR_ERROR
  127.     * @param int $expires lifetime of the cached data in seconds - 0 for endless
  128.     * @see Cache, HTTP_Request
  129.     * @access public
  130.     */
  131.     function Cache_HTTP_Request($url, $params = NULL, $container  = 'file',
  132.                                 $containerOptions = NULL, $expires = 3600,
  133.                                 $mode = CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY)
  134.     {
  135.         if (!isset($params)) $params = array();
  136.         if (!isset($containerOptions)) {
  137.             $containerOptions = array (
  138.                 'cache_dir' => '/tmp/',
  139.                 'filename_prefix' => 'cache_'
  140.             );
  141.         }
  142.         $this->Cache($container, $containerOptions);
  143.         $this->_request = &new HTTP_Request($url, $params);
  144.         $this->_id = md5($url.serialize($params));
  145.         $this->_id2 = md5($this->_id); // we need two keys
  146.         $this->_mode = $mode;
  147.         $this->_expires = $expires;
  148.     }
  149.  
  150.     /**
  151.     * Deconstructor
  152.     *
  153.     * @access public
  154.     */
  155.     function _Cache_HTTP_Request()
  156.     {
  157.         $this->_Cache();
  158.     }
  159.  
  160.     /**
  161.     * Get and return the response body (NULL if no data available)
  162.     *
  163.     * @see sendRequest()
  164.     * @return mixed response body
  165.     * @access public
  166.     */
  167.     function getResponseBody()
  168.     {
  169.         return $this->_data['body'];
  170.     }
  171.  
  172.     /**
  173.     * Get and return the response code (NULL if no data available)
  174.     *
  175.     * @see sendRequest()
  176.     * @return mixed response code
  177.     * @access public
  178.     */
  179.     function getResponseCode()
  180.     {
  181.         return $this->_data['code'];
  182.     }
  183.  
  184.     /**
  185.     * Get and return the response header (NULL if no data available)
  186.     *
  187.     * @see sendRequest()
  188.     * @return mixed response header
  189.     * @access public
  190.     */
  191.     function getResponseHeader()
  192.     {
  193.         return $this->_data['header'];
  194.     }
  195.  
  196.  
  197.     /**
  198.     * Set a new mode when the server is down
  199.     *
  200.     * @param int $newMode What to do when the remote server is down :
  201.     *                      CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY or
  202.     *                      CACHE_HTTP_REQUEST_RETURN_FALSE or
  203.     *                      CACHE_HTTP_REQUEST_RETURN_PEAR_ERROR
  204.     * @access public
  205.     */
  206.     function setMode($newMode)
  207.     {
  208.         $this->_mode = $newMode;
  209.     }
  210.  
  211.     /**
  212.     * Send the HTTP request or use the cache system
  213.     *
  214.     * If there is a cache file for this HTTP request, the request is not re-sent.
  215.     * Cached response is used. Yet, if the cache is expired, the HTTP request
  216.     * is re-sent. Then, if the remote server is down, this method will return :
  217.     * (depending on the selected mode)
  218.     * - false or
  219.     * - a PEAR_Error or (better)
  220.     * - true and the local copy of the latest valid response will be used.
  221.     *
  222.     * (technical)
  223.     * For the last choice, there is a technical tips.
  224.     * Indeed, there are two cache files. The first one (id key) is a classical one
  225.     * with the given lifetime. But it can be removed by automatic garbage collection
  226.     * for example. So to be able to use the latest valid response (when the remote
  227.     * server is dead), we make a second cache file with no lifetime (id2 key).
  228.     *
  229.     * @return mixed true or false or a PEAR_ERROR
  230.     * @access public
  231.     */
  232.     function sendRequest()
  233.     {
  234.         if ($data = $this->get($this->_id, CACHE_HTTP_REQUEST_GROUP_NAME)) {
  235.             // --- Cache hit ---
  236.             $this->_data = $data;
  237.             return true;
  238.         } else {
  239.             // --- Cache miss ---
  240.             if ($this->_sendRequestAndGetResponse()) {
  241.                 // So the remote server is ok...
  242.                 $this->save($this->_id, $this->_data, $this->_expires, CACHE_HTTP_REQUEST_GROUP_NAME);
  243.                 $this->save($this->_id2, $this->_data, 0, CACHE_HTTP_REQUEST_GROUP_NAME);
  244.                 return true;
  245.             } else {
  246.                 if ($data_sav = $this->get($this->_id2, CACHE_HTTP_REQUEST_GROUP_NAME)) {
  247.                     // Ok, the "recover cache" is available...
  248.                     switch ($this->_mode) {
  249.                     case CACHE_HTTP_REQUEST_KEEP_LOCAL_COPY:
  250.                         // We make a new local copy and keep it until it expires...
  251.                         $this->save($this->_id, $data_sav, $this->_expires, CACHE_HTTP_REQUEST_GROUP_NAME);
  252.                         $this->_data = $data_sav;
  253.                         return true;
  254.                         break;
  255.                     case CACHE_HTTP_REQUEST_RETURN_FALSE:
  256.                         // We return false
  257.                         return false;
  258.                         break;
  259.                     case CACHE_HTTP_REQUEST_RETURN_PEAR_ERROR:
  260.                         // We return a PEAR_Error!
  261.                         return new Cache_Error('Remote file is not available!');
  262.                         break;
  263.                     }
  264.                 } else {
  265.                     // It's terrible! The remote server is down and definitively no cache available!
  266.                     return new Cache_Error('Remote server down and no cache available!');
  267.                 }
  268.             }
  269.         }
  270.     }
  271.  
  272.     // --- Private Methods ---
  273.  
  274.     /**
  275.     * Send HTTP request and get the response
  276.     *
  277.     * @return boolean success or not ?
  278.     * @see HTTP_Request
  279.     * @access private
  280.     */
  281.     function _sendRequestAndGetResponse()
  282.     {
  283.         $this->_request->sendRequest();
  284.         $body = $this->_request->getResponseBody();
  285.         $code = $this->_request->getResponseCode();
  286.         $header = $this->_request->getResponseHeader();
  287.         $this->_data = array(
  288.             'body' => $body,
  289.             'code' => $code,
  290.             'header' => $header
  291.         );
  292.         return (($code==CACHE_HTTP_REQUEST_SUCCESS_RESPONSE_CODE) ? true : false);
  293.     }
  294.  
  295. }
  296. ?>
  297.